/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.desktop.paneles;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.DefaultListModel;

/**
 *
 * @author Yanira
 */
public class Estadisticas extends javax.swing.JPanel {

    /**
     * Creates new form Estadisticas
     */
    private AdmEvent padre;
    private DefaultListModel lista = new DefaultListModel();
    
    public Estadisticas(AdmEvent padre) {
        initComponents();
        this.padre = padre;
        
        setEstadisticas();
    }
    
    public void setEstadisticas()
    {
        this.jLabel33.setText(Temporal.evento);
        Gson gson = new Gson();
        String datos = listaEstadistica(Temporal.evento);
        String [] datosParseados = gson.fromJson(datos, String[].class);
        if(datosParseados != null)
        {
            if(!(datosParseados[0]).equals("-1"))
            {
                this.jLabel3.setText(datosParseados[0]);
            }
            else
            {
                this.jLabel3.setText("0, a nadie le gusta esto");
            }
            if(!(datosParseados[1]).equals("-1") )
            {
                this.jLabel5.setText(datosParseados[1]);
            }
            else
            {
                this.jLabel5.setText("0, nadie ha comentado");
            }
            if(!(datosParseados[3]).equals("-1") )
            {
                this.jLabel10.setText(datosParseados[3]);
                this.jLabel8.setText(datosParseados[2]);
            }
            else
            {
                this.jLabel10.setText("0");
                this.jLabel8.setText("ninguno");
            }
            if(!(datosParseados[5]).equals("-1") )
            {
                this.jLabel15.setText(datosParseados[5]);
                this.jLabel13.setText(datosParseados[4]);
            }
            else
            {
                this.jLabel15.setText("0");
                this.jLabel13.setText("ninguno");
            }
            if(!(datosParseados[8]).equals("-1") )
            {
                this.jLabel19.setText(datosParseados[6]);
                this.jLabel21.setText(datosParseados[7]);
                this.jLabel23.setText(datosParseados[8]);
            }
            else
            {
                this.jLabel23.setText("0");
                this.jLabel21.setText("nadie");
                this.jLabel19.setText("ninguna");
            }
            if(!(datosParseados[11]).equals("-1") )
            {
                this.jLabel26.setText(datosParseados[9]);
                this.jLabel28.setText(datosParseados[10]);
                this.jLabel30.setText(datosParseados[11]);
            }
            else
            {
                this.jLabel30.setText("0");
                this.jLabel28.setText("nadie");
                this.jLabel26.setText("ninguna");
            }
         
            //lista
           
            Type collectionType = new TypeToken<HashMap<String,Integer>>(){}.getType();
            HashMap<String,Integer>lista2 = gson.fromJson(datosParseados[12],collectionType) ;
            lista.addElement("Actividades con cupos sobrantes");
            if(lista2.isEmpty())
                lista.addElement("No existen actividades con problemas de cupo");
            else
            {
                Iterator it = lista2.keySet().iterator();
                while(it.hasNext())
                {
                    String value = (String) it.next();
                    lista.addElement(value);
                }
                     
            }
            
            lista.addElement("Actividades con problemas de horario");
            
            if(conflictoActividades()==null)
            {
                lista.addElement("No existen actividades con problemas de horario");
            }
            else
            {
                String[] listaCS = gson.fromJson(conflictoActividades(), String[].class);
                for(String c: listaCS)
                {
                    lista.addElement(c);
                }
            }
            jList1.setModel(lista);
            
        }
        
    }
    
    public String conflictoActividades()
    {
        Gson gson = new Gson();
        DefaultListModel actividadesConflic = new DefaultListModel();
        String[] actividadesEvento = gson.fromJson(listarActividades(Temporal.evento), String[].class);
        if(actividadesEvento!=null)
        {
            for(String a: actividadesEvento)
            {
                String[] datosActividad = gson.fromJson(datosActividad(Temporal.evento,a), String[].class);
                Calendar fechaInicio = gson.fromJson(datosActividad[4], Calendar.class);
                Calendar fechaFin = gson.fromJson(datosActividad[5],Calendar.class);
                if(actividadEnEv(fechaInicio,fechaFin))
                {
                    //si esta en rango de fechas no pasa nada
                }
                else
                {
                    //si se sale del rango se añade a la lista
                    actividadesConflic.addElement(a);
                }
            }
            if(actividadesConflic.getSize()>0)
            {
                String[] conflictos = new String[actividadesConflic.size()];
                Object[] actividadesConfli = actividadesConflic.toArray();
                for(int i=0; i<actividadesConflic.size(); i++)
                {
                    conflictos[i]=actividadesConfli[i].toString();
                }
                String actConflic = gson.toJson(conflictos);
                return actConflic;
            }
            return null;
        }
        return null;
    }
    
    
    public boolean actividadEnEv (Calendar inicioActv, Calendar finActv) 
    {
        Gson gson2 = new Gson();
        String fechas =  obtenerDatosEvento(Temporal.evento);
        String [] fechasParseadas = gson2.fromJson(fechas, String[].class);
        
        Calendar inicioEv = gson2.fromJson(fechasParseadas[2], Calendar.class);
        Calendar finEv = gson2.fromJson(fechasParseadas[3], Calendar.class);                
               
        if((inicioEv.before(inicioActv) && finEv.after(finActv)) || (inicioEv.equals(inicioActv) && finEv.after(finActv))  || (finEv.equals(finActv) && inicioEv.before(inicioActv)) || (finEv.equals(finActv) && inicioEv.equals(inicioActv))  )
        {
            return true;
        }
        else{
            return false;
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        jLabel22 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        jLabel24 = new javax.swing.JLabel();
        jLabel25 = new javax.swing.JLabel();
        jLabel26 = new javax.swing.JLabel();
        jLabel27 = new javax.swing.JLabel();
        jLabel28 = new javax.swing.JLabel();
        jLabel29 = new javax.swing.JLabel();
        jLabel30 = new javax.swing.JLabel();
        jLabel31 = new javax.swing.JLabel();
        jLabel32 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        jLabel33 = new javax.swing.JLabel();

        setMaximumSize(new java.awt.Dimension(602, 548));
        setMinimumSize(new java.awt.Dimension(602, 548));
        setPreferredSize(new java.awt.Dimension(602, 548));

        jLabel1.setText("Evento:");

        jLabel2.setText("Cantidad de \"Likes\":");

        jLabel3.setText("jLabel3");

        jLabel4.setText("Cantidad de Comentarios:");

        jLabel5.setText("jLabel5");

        jLabel6.setText("Actividades:");

        jLabel7.setText("La Actvidad Más Comentada es ");

        jLabel8.setText("jLabel8");

        jLabel9.setText("Con ");

        jLabel10.setText("jLabel10");

        jLabel11.setText("Comentarios");

        jLabel12.setText("La Actividad con más \"Likes\" es ");

        jLabel13.setText("jLabel13");

        jLabel14.setText("Con");

        jLabel15.setText("jLabel15");

        jLabel16.setText("\"Likes\"");

        jLabel17.setText("Expositores:");

        jLabel18.setText("La actividad en que el expositor recibió más \"Likes\" es ");

        jLabel19.setText("jLabel19");

        jLabel20.setText("El expositor con más \"Likes\" es ");

        jLabel21.setText("jLabel21");

        jLabel22.setText("Con un total de ");

        jLabel23.setText("jLabel23");

        jLabel24.setText("\"Likes\"");

        jLabel25.setText("La actividad en que el expositor recibió más comentarios es ");

        jLabel26.setText("jLabel26");

        jLabel27.setText("El expositor con más comentarios es ");

        jLabel28.setText("jLabel28");

        jLabel29.setText("Con un total de ");

        jLabel30.setText("jLabel30");

        jLabel31.setText("Comentarios");

        jLabel32.setText("Las actividades con cupos faltantes o con problemas de horario:");

        jScrollPane1.setViewportView(jList1);

        jButton1.setText("Atrás");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel33.setText("jLabel33");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel20)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel21, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel14)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel15)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel16))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel18)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel19)))
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel12)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel22)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel23)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel24, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel25)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel26, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)))
                        .addGap(173, 173, 173))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel33, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel32)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel9)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel10)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel11))
                                    .addComponent(jLabel17)
                                    .addComponent(jLabel6))
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel27)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel28, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(173, 173, 173))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel29)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel30)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel31)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jButton1))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel33))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(jLabel13))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel14)
                    .addComponent(jLabel15)
                    .addComponent(jLabel16))
                .addGap(18, 18, 18)
                .addComponent(jLabel17)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel18)
                    .addComponent(jLabel19))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel20)
                    .addComponent(jLabel21))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel22)
                    .addComponent(jLabel23)
                    .addComponent(jLabel24))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel25)
                    .addComponent(jLabel26))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel27)
                    .addComponent(jLabel28))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel29)
                    .addComponent(jLabel30)
                    .addComponent(jLabel31))
                .addGap(18, 18, 18)
                .addComponent(jLabel32)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.padre.jSplitPane2.setRightComponent(new InformEvento(Temporal.evento,padre ));
        
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JLabel jLabel27;
    private javax.swing.JLabel jLabel28;
    private javax.swing.JLabel jLabel29;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel30;
    private javax.swing.JLabel jLabel31;
    private javax.swing.JLabel jLabel32;
    private javax.swing.JLabel jLabel33;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    

  

    

    private static String listaEstadistica(java.lang.String evento) {
        com.webservice.servicios.Gestor_Service service = new com.webservice.servicios.Gestor_Service();
        com.webservice.servicios.Gestor port = service.getGestorPort();
        return port.listaEstadistica(evento);
    }

    private static String obtenerDatosEvento(java.lang.String evento) {
        com.webservice.servicios.Gestor_Service service = new com.webservice.servicios.Gestor_Service();
        com.webservice.servicios.Gestor port = service.getGestorPort();
        return port.obtenerDatosEvento(evento);
    }

    private static String datosActividad(java.lang.String evento, java.lang.String actividad) {
        com.webservice.servicios.Gestor_Service service = new com.webservice.servicios.Gestor_Service();
        com.webservice.servicios.Gestor port = service.getGestorPort();
        return port.datosActividad(evento, actividad);
    }

    private static String listarActividades(java.lang.String evento) {
        com.webservice.servicios.Gestor_Service service = new com.webservice.servicios.Gestor_Service();
        com.webservice.servicios.Gestor port = service.getGestorPort();
        return port.listarActividades(evento);
    }


}
